package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class ServiceItemServiceImpl implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;

    @Autowired
    private IBpmnInfoService bpmnInfoService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private ICarPackageAuditService carPackageAuditService;

    @Autowired
    private IUserService userService;


    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<ServiceItem>(serviceItemMapper.selectForList(qo));
    }


    @Override
    public void save(ServiceItem serviceItem) {
        serviceItem.setCreateTime(new Date());
        serviceItem.setSaleStatus(ServiceItem.SALESTATUS_OFF);
        if(ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())){
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }else {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_NO_REQUIRED);
        }
        serviceItemMapper.insert(serviceItem);
    }

    @Override
    public ServiceItem get(Long id) {
        return serviceItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(ServiceItem serviceItem) {

        ServiceItem item = serviceItemMapper.selectByPrimaryKey(serviceItem.getId());

        if(ServiceItem.SALESTATUS_ON.equals(item.getSaleStatus())){
            throw new BusinessException("上架中的服务,不能修改");
        }
        if(ServiceItem.AUDITSTATUS_AUDITING.equals(item.getAuditStatus())){
            throw new BusinessException("审核中的服务,不能修改");
        }

        if(ServiceItem.CARPACKAGE_YES.equals(item.getCarPackage())){
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }else {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_NO_REQUIRED);
        }

        serviceItemMapper.updateByPrimaryKey(serviceItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            serviceItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<ServiceItem> list() {
        return serviceItemMapper.selectAll();
    }

    @Override
    public void saleStatusOn(Long id) {

        ServiceItem serviceItem = serviceItemMapper.selectByPrimaryKey(id);

        if((ServiceItem.SALESTATUS_ON.equals(serviceItem.getSaleStatus()))){
            throw new BusinessException("该服务已经上架");
        }


        if((ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())&&!ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus()))){
            throw new BusinessException("套餐必须审核后才能上架");
        }



        serviceItemMapper.updateSaleStatusById(id,ServiceItem.SALESTATUS_ON,null);
    }

    @Override
    public void saleStatusOff(Long id) {

        ServiceItem serviceItem = serviceItemMapper.selectByPrimaryKey(id);

        if((ServiceItem.SALESTATUS_OFF.equals(serviceItem.getSaleStatus()))){
            throw new BusinessException("该服务已经下架");
        }

        if(ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())){
            serviceItemMapper.updateSaleStatusById(id,ServiceItem.SALESTATUS_OFF,ServiceItem.AUDITSTATUS_INIT);
        }

        if(ServiceItem.CARPACKAGE_NO.equals(serviceItem.getCarPackage())){
            serviceItemMapper.updateSaleStatusById(id,ServiceItem.SALESTATUS_OFF,null);
        }


    }

    @Override
    public void startAudit(Long id, Long bpmnInfoId, Long director, Long finance, String info) {
        if(id==null||bpmnInfoId==null||director==null){
            throw new BusinessException("传入参数异常");
        }
        ServiceItem serviceItem = serviceItemMapper.selectByPrimaryKey(id);
        if(ServiceItem.CARPACKAGE_NO.equals(serviceItem.getCarPackage())){
            throw new BusinessException("非套餐无需发起审批");
        }
        if(ServiceItem.SALESTATUS_ON.equals(serviceItem.getSaleStatus())){
            throw new BusinessException("已经上架产品无法发起审批");
        }
        if(!ServiceItem.AUDITSTATUS_INIT.equals(serviceItem.getAuditStatus())){
            throw new BusinessException("非初始化订单无法发起审批");
        }
        if(userService.get(director)==null){
            throw new BusinessException("传入参数异常");
        }
        BpmnInfo bpmnInfo = bpmnInfoService.get(bpmnInfoId);
        if(bpmnInfo==null){
            throw new BusinessException("传入参数异常");
        }

        //发起审核业务流程
        CarPackageAudit carPackageAudit = new CarPackageAudit();
        carPackageAudit.setCreateTime(new Date());
        carPackageAudit.setBpmnInfoId(bpmnInfoId);
        carPackageAudit.setServiceItemId(id);
        carPackageAudit.setServiceItemPrice(serviceItem.getDiscountPrice());
        carPackageAudit.setAuditorId(director);
        carPackageAudit.setInfo(info);
        carPackageAudit.setServiceItemInfo(serviceItem.getInfo());
        carPackageAudit.setStatus(CarPackageAudit.STATUS_IN_ROGRESS);
        carPackageAudit.setCreator(ShiroUtils.getUser().getUserName());
        carPackageAuditService.save(carPackageAudit);
        serviceItemMapper.updateSaleStatusById(id,null,ServiceItem.AUDITSTATUS_AUDITING);

        //发起审核activiti7
        Map<String, Object> map = new HashedMap<>();
        map.put("director",director);
        map.put("finance",finance);
        map.put("discountPrice",serviceItem.getDiscountPrice().longValue());

        ProcessInstance processInstance = runtimeService.startProcessInstanceById(bpmnInfo.getActProcessId(), carPackageAudit.getId().toString(), map);
        carPackageAudit.setInstanceId(processInstance.getId());
        carPackageAuditService.update(carPackageAudit);

    }

    @Override
    public void setStatus(Long id, Integer auditstatus) {
        serviceItemMapper.updateSaleStatusById(id,null,auditstatus);
    }


}
